﻿using K8.CBB;
using KingerRobot.PCRMES.Dao;
using KingerRobot.PCRMES.Spi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.PCRMES.SpiService;

public class MesOrderImpl: IMesOrderService
{
    IWmsProductionRepositories wmsProductionRepositories = new WmsProductionRepositories();
    IWmsLockRepositories wmsLockRepositories = new WmsLockRepositories();
    IWmsOrderRepositories wmsOrderRepositories = new WmsOrderRepositories();
    IWmsOutRepositories wmsOutRepositories = new WmsOutRepositories();
    IWmsCheckRepositories _wmsCheckRepositories = new WmsCheckRepositories();

    public List<i_wms_lock> QueryMesLocks()
    {
        Expression<Func<i_wms_lock, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.UPDATE_FLAG == 1);
        return  wmsLockRepositories.Query(whereExpression);
    }

    public List<i_wms_order> QueryMesOrder()
    {
        Expression<Func<i_wms_order, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.UPDATE_FLAG == 1);
        
        return wmsOrderRepositories.Query(whereExpression);
    }

    public List<i_wms_production> QueryMesProductions()
    {
        Expression<Func<i_wms_production, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.UPDATE_FLAG == 1 && d.INNERDIAMETER !=null);

        return wmsProductionRepositories.Query(whereExpression);
    }

    public int SaveWmsOut(i_wms_out iWmsOut)
    {
        Expression<Func<i_wms_out, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d =>  d.OUT_ID == iWmsOut.OUT_ID);

        var i_wms_out = wmsOutRepositories.First(whereExpression);
        if (i_wms_out!=null)
        {
            return wmsOutRepositories.Update(iWmsOut);
        }
        else
        {
            return wmsOutRepositories.Add(iWmsOut);
        } 
    }

    public int UpdateMesProduction(i_wms_production wms_product)
    {
        Expression<Func<i_wms_production, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.PRODUCTION_ID == wms_product.PRODUCTION_ID);
        return wmsProductionRepositories.Update(whereExpression, wms_product);
    }

    public int UpdateWmsLock(i_wms_lock wmsLock)
    {
        Expression<Func<i_wms_lock, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.LOCK_ID == wmsLock.LOCK_ID);
        return wmsLockRepositories.Update(whereExpression,wmsLock);
    }

    public int UpdateWmsOrder(i_wms_order wmsOrder)
    {
        Expression<Func<i_wms_order, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.LIST_NO == wmsOrder.LIST_NO);
        return wmsOrderRepositories.Update(whereExpression,wmsOrder);
    }
   
    public int ClearMesOutOrders(string orderId)
    {
        return wmsOutRepositories.DeleteById(orderId);
    }

    public int ClearMesProductions(string productId)
    {
        return wmsProductionRepositories.DeleteById(productId);
    }


    public List<i_wms_check> QueryWmsCheck()
    {
        Expression<Func<i_wms_check, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.update_flag == 1 && d.check_result=="1");
        return _wmsCheckRepositories.Query(whereExpression);
    }
    public int UpdateWmsCheck(i_wms_check check)
    {
        Expression<Func<i_wms_check, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.check_id == check.check_id);
        return _wmsCheckRepositories.Update(whereExpression, check);
    }
}
